#!/bin/sh

ERTM_OVERRIDE="/etc/modprobe.d/99-xpadneo-bluetooth.conf"
DISABLE_ERTM="/sys/module/bluetooth/parameters/disable_ertm"

if [ "$(readlink "${ERTM_OVERRIDE}" 2>/dev/null)" = "/dev/null" ]; then
	echo "Not disabling ERTM, local override in place..."
elif [ -L "${ERTM_OVERRIDE}" ]; then
	echo >&2 "WARNING: '${ERTM_OVERRIDE}' is an arbitrary symlink, this is not supported."
	echo >&2 "Skipping ERTM fixup, you're expected to manage the options on your own..."
else
	echo "Disabling ERTM permanently (requires reboot)..."
	echo "options bluetooth disable_ertm=y" >"${ERTM_OVERRIDE}"
	echo "HINT: If you want to prevent this in the future, run 'ln -snf /dev/null ${ERTM_OVERRIDE}'."

	if [ -w "${DISABLE_ERTM}" ]; then
		echo "Disabling ERTM instantly..."
		echo "Y" >"${DISABLE_ERTM}"
	fi
fi

echo "Installing modalias database..."
install -D -t /etc/modprobe.d etc-modprobe.d/xpadneo.conf

echo "Installing udev rules..."
install -D -t /etc/udev/rules.d etc-udev-rules.d/98-xpadneo.rules

echo "Reloading udev..."
udevadm control --reload
